const Base = require('./base.js');
const ErrCode = require('../utils/errCode');
const Status = require('../utils/status');
const XLSX = require("xlsx");

module.exports = class extends Base {

    addStudentByExcelAction() {
        const xlsfile = this.file("studentXlsFile");
        const { classid, subjectid } = this.post();
        let workbook = XLSX.readFile(xlsfile.path); // xls 文档对象
        let sheetNames = workbook.SheetNames; // 获取所有表名
        let sheet = workbook.Sheets[sheetNames[0]] // 获取xls文档第一个表对象
        var studentList = XLSX.utils.sheet_to_json(sheet) // 获取表对象数据并转成json
        studentList.forEach(async(item) => {
            console.log({ id: item['学号'], studentname: item['姓名'], position: item['职务'], phone: item['电话'], classid });
            await this.model('student').add({ id: item['学号'], studentname: item['姓名'], position: item['职务'], phone: item['电话'], classid }, { replace: true });
            await this.model('student_subject').add({ studentid: item['学号'], subjectid, classid, state: 1 }); // 1 表示必修
        });
        return this.success({}, Status.STUDENT_ADD_SUCCESS);
    }

    async getAllStudentWithSubjectStatusAction() {
        const { classid, subjectid } = this.post();
        let classInfo = await this.model('class').where({ id: classid }).find();
        let studentList = await this.model('student').where({ classid }).select();
        let subjectStates = await this.model('student_subject').where({ classid, subjectid }).select();
        studentList.forEach((item) => {
            item.classname = classInfo.classname;
            subjectStates.forEach((it) => {
                    if (it.studentid == item.id) item.state = it.state;
                })
                // console.log(item)
        });
        return this.success(studentList, Status.STUDENT_UPDATE_SUCCESS);
    }

    async updateStudentInfoAction() {
        const { classid, subjectid, studentid, studentname, position, phone } = this.post();
        let student = await this.model('student').where({ id: studentid }).find();
        if (JSON.stringify(student) === "{}") {
            await this.model('student').add({ id: studentid, studentname, position, phone, classid });
            await this.model('student_subject').add({ subjectid, classid, studentid, state: 1 });
        } else {
            await this.model('student').where({ id: studentid }).update({ studentname, position, phone });
        }
        return this.success({}, Status.STUDENT_UPDATE_SUCCESS);
    }

    async deleteStudentAction() {
        const { studentid } = this.get();
        await this.model('student').where({ id: studentid }).delete();
        await this.model('student_subject').where({ studentid }).delete();
        return this.success({}, Status.STUDENT_DELETE_SUCCESS);
    }

    async updateSubjectStateAction() {
        const { stateValue, studentid, subjectid } = this.post();
        let res = await this.model('student_subject').where({ studentid, subjectid }).update({ state: stateValue });
        console.log(res)
        return this.success({}, Status.STUDENT_UPDATE_SUCCESS);
    }

};